package com.etlmaster.transformer.funcs;

import java.util.Map;

import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.trans.core.Line;
import com.etlmaster.executor.trans.core.LineTransProcessor;
import com.etlmaster.executor.utils.LogWriter;

/**
 * 行级处理 编码转换
 * @author wugf
 * @date 2016-04-29
 */
@StepInstruction("codeMap")
public class TransFieldByCodeMap extends LineTransProcessor{

	private Map codeMap;
	private String codeMapName;
	private String insertType;
	private String nonExistPolocy;
	private String constantValue;
	public TransFieldByCodeMap(String sourceTab,String sourceCol,String targetTab,String targetCol,String changeCode,Map paramStr,Map var){
		
		super( sourceTab,sourceCol,targetTab,targetCol,changeCode, paramStr,var);
	
	}

	public void changeCode(Line line,String targetValue){
		if(insertType.equals("replace")){
			line.replaceField(targetValue, this.getFieldIndex(line));
		}else{
			//line.addField(targetValue);
		}
	}
	public void changeNonExistCode(Line line){
		if(nonExistPolocy.equals("constant")){
			changeCode(line,"constant");
		}else if(nonExistPolocy.equals("delete")){
			line.setSkip(true);
		}else{
			LogWriter.addLog("WARN","不支持的转换类型:{}",nonExistPolocy);
		}
	}

	public boolean isSkip() {
		
		return false;
	}
	@Override
	public void transField(Line line) {
		String sourceValue=line.getField(this.getFieldIndex(line));
		if(codeMap.containsKey(sourceValue)){
			changeCode(line,(String) codeMap.get(sourceValue));
		}else{
			changeNonExistCode(line);
		}
		
	}

}
